package com.qili.config;


import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.repository.support.AbstractElasticsearchRepository;

/**
 * @author zhaoyongke
 * @date 2021/10/7
 * @description
 */
@Configuration
public class ElasticsearchConfig   {
    /**
     * ES地址,ip:port
     */
    @Value("${elasticsearch.ip}")
    String ipPort;
    public   static final RequestOptions COMMON_OPTIONS;
    static {
        RequestOptions.Builder builder=RequestOptions.DEFAULT.toBuilder();
        COMMON_OPTIONS=builder.build();
    }
    @Bean
    public RestHighLevelClient restClientBuilder() {
        RestHighLevelClient  restHighLevelClient=new RestHighLevelClient(
                RestClient.builder(makeHttpHost(ipPort))
        );

        return  restHighLevelClient;
    }

    private HttpHost makeHttpHost(String s) {
        String[] address = s.split(":");
        String ip = address[0];
        int port = Integer.parseInt(address[1]);

        return new HttpHost(ip, port, "http");
    }
}
